package com.east.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.persistence.Transient;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.east.bean.Menu;
import com.east.bean.Privilege;
import com.east.bean.PrivilegeRole;
import com.east.bean.Role;
import com.east.dao.MenuMapper;
import com.east.dao.PrivilegeMapper;
import com.east.dao.PrivilegeRoleMapper;
import com.east.dao.RoleMapper;
import com.east.service.system.MenuService;
import com.east.utils.ObjectUtil;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import redis.clients.jedis.Connection;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service("menuService")
public class MenuServiceImpl implements MenuService{
	@Autowired
	MenuMapper menuMapper;
	@Autowired
	RoleMapper roleMapper;
	@Autowired
	PrivilegeMapper privilegeMapper;
	@Autowired
	PrivilegeRoleMapper privilegeRoleMapper;
	@Override
	public List<Menu> findByUserId(Integer id) {
		return menuMapper.findByUserId(null);
	}
	@Override
	public Page<Role> findByRole(Pagenation pagenation) {
		Page<Role> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());		
		Example example = new Example(Role.class);
		example.orderBy("edittime").desc();
		roleMapper.selectByExample(example);
		return startPage;
	}
	@Override
	public List<Privilege> findByRolePriv(Integer roleId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("roleIds", roleId);
		return privilegeMapper.findByRolePriv(map);
	}
	@Override
	public List<Privilege> findByPrivAll() {
		return privilegeMapper.findByPrivAll();
	}
	@Override
	public List<Privilege> findPrivMenu() {
		return null;
	}
	@Override
	public int savePrivilege(Privilege privilege) {
		return privilegeMapper.insert(privilege);
	}
	@Override
	public int updatePrivilege(Privilege privilege) {
		Privilege selectByPrimaryKey = privilegeMapper.selectByPrimaryKey(privilege.getpId());
		BeanUtils.copyProperties(privilege, selectByPrimaryKey);
		int updateByPrimaryKey = privilegeMapper.updateByPrimaryKey(privilege);
		return updateByPrimaryKey;
	}
	@Override
	public int delPrivilege(Privilege privilege) {
		int deleteByPrimaryKey = privilegeMapper.deleteByPrimaryKey(privilege.getpId());
		return deleteByPrimaryKey;
	}
	@Override
	public Privilege findByPrivilegeId(Privilege privilege) {
		return privilegeMapper.selectByPrimaryKey(privilege.getpId());
	}
	@Override
	public List<Privilege> findByprivRoleAll() {
		return privilegeMapper.findByPrivRole();
	}
	@Transactional
	@Override
	public int saveRole(String[] pIds, String name, String remarks) {
		Role role = new Role();
		role.setAddtime(new Date());
		role.setName(name);
		role.setEdittime(new Date());
		role.setRemarks(remarks);
		roleMapper.insertRole(role);
		List<PrivilegeRole> list=new ArrayList<PrivilegeRole>();
		for (String string : pIds) {
			PrivilegeRole pr = new PrivilegeRole();
			pr.settPId(Integer.valueOf(string));
			pr.settRId(role.getId());
			list.add(pr);
		}
		int insert = privilegeRoleMapper.savePrivilegeRoleList(list);
		return insert;
	}
	@Override
	public Role findByRolePrivilege(Role role) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", role.getId());
		//已有权限
		List<Integer> findByRolePriv = privilegeMapper.findByRolePrivInteger(map);
		System.out.println(JSON.toJSON(findByRolePriv));
		//所有权限
		 List<Privilege> findByPrivRole = privilegeMapper.findByPrivRole();
		 System.out.println(JSON.toJSON(findByRolePriv));
		 System.out.println(JSON.toJSON(findByPrivRole));
		 //合并
		 for (Privilege pr : findByPrivRole) {
			 if(ObjectUtil.beanbjList(findByRolePriv, pr.getpId())) {
				 pr.setChecked(true);
			 }
			for (Privilege privilege : pr.getPrivileges()) {
				 if(ObjectUtil.beanbjList(findByRolePriv, privilege.getpId())) {
					 privilege.setChecked(true);
				 }
			}
		 }
		 Role selectByPrimaryKey = roleMapper.selectByPrimaryKey(role.getId());
		 selectByPrimaryKey.setPrivileges(findByPrivRole);
		return selectByPrimaryKey;
	}
	@Transactional
	@Override
	public int updateRole(String[] pIds, String name, String remarks, Integer id) {
		Role role = roleMapper.selectByPrimaryKey(id);
		role.setName(name);
		role.setRemarks(remarks);
		role.setEdittime(new Date());
		roleMapper.updateByPrimaryKey(role);
		//先删除后增加
		 privilegeRoleMapper.delByRoleId(id);
		List<PrivilegeRole> list=new ArrayList<PrivilegeRole>();
		for (String string : pIds) {
			PrivilegeRole pr = new PrivilegeRole();
			pr.settPId(Integer.valueOf(string));
			pr.settRId(role.getId());
			list.add(pr);
		}
		int insert = privilegeRoleMapper.savePrivilegeRoleList(list);
		return insert;
	}
	@Transactional
	@Override
	public int delRole(Integer id) {
		privilegeRoleMapper.delByRoleId(id);
		int deleteByPrimaryKey = roleMapper.deleteByPrimaryKey(id);
		return deleteByPrimaryKey;
	}
}
